#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>

using namespace std;

//int main()
//{
//    int n = 1024, m = 19, j = 2, i = 6;
//    int len = i - j + 1;
//    for (int a = 0; a < len; a++)
//    {
//        n |= ((1 & (m >> a)) << (j + a));
//    }
//    cout << n;
//	return 0;
//}

//int main()
//{
//	char* p = NULL;
//	char*& a = p;
//	return 0;
//}

#include <cmath>
#include <vector>
#include <climits>
#include <cstring>

using namespace std;

#define N 100010

void get_y(vector<int>& v, int n) {
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0) {
            v.push_back(i);
            if (i != (n / i))
                v.push_back(n / i);
        }
    }
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> dp(m + 1, INT_MAX);
    dp[n] = 0;
    for (int i = n; i <= m; i++) {
        vector<int> tmp;
        get_y(tmp, i);
        for (int j = 0; j < tmp.size(); j++) {
            if (i + tmp[j] <= m) {
                int z = i + tmp[j];
                if(dp[i] != INT_MAX)
                    dp[z] = min(dp[z], dp[i] + 1);
            }
        }
    }
    if (dp[m] != INT_MAX)
        cout << dp[m];
    else cout << -1;
    return 0;
}